/**
 * @Author Fizz Pu
 * @Date 2021/3/6 下午5:39
 * @Version 1.0
 * 失之毫厘，缪之千里！
 */

import java.util.*;

public class Leeoffer3 {

    public static void main(String[] args) {
        List<List<Integer>> lists = new Leeoffer3().subsetsWithDup(new int[]{
                4, 4, 4 , 1, 4
        });

        System.out.println(lists);
    }

        public List<List<Integer>> subsetsWithDup(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
            List<Integer> inner = new ArrayList<>();
            res.add(inner);
            dfs(0, nums, res, inner);
            return res;
        }


        private void dfs(int start, int[] nums, List<List<Integer>> res, List<Integer> tmp){

            if(start == nums.length){
                return;
            }

            // 可行解去重复
            Set<Integer> set = new HashSet<>();
            for(int i = start; i < nums.length; i++){

                if(set.contains(nums[i])){
                    continue;
                }

                set.add(nums[i]);
                tmp.add(nums[i]);
                res.add(new ArrayList<Integer>(tmp));
                dfs(i + 1, nums, res, tmp);
                tmp.remove(tmp.size()-1);
            }
        }
    }
